package com.link2room.blaster.facade.impl;

import com.link2room.blaster.constant.MsgConstant;
import com.link2room.blaster.dto.innerUse.SMSAuthCodeParamDto;
import com.link2room.blaster.dto.innerUse.VerifyCodeParamDto;
import com.link2room.blaster.dto.result.SMSAuthCodeResDto;
import com.link2room.blaster.common.FreemarkerUtil;
import com.link2room.blaster.facade.ISMSCodeFacadeService;
import com.link2room.blaster.service.IMsgTemplateService;
import com.link2room.blaster.util.SMSUtil;
import com.link2room.common.dto.result.base.L2RGroupBaseInfoDto;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.exception.SysException;
import com.link2room.common.util.HotelUtil;
import com.link2room.common.util.StringUtil;
import com.link2room.blaster.entity.MsgTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by LN on 2017/4/18.
 */
@Component("smsCodeFacadeService")
public class SMSCodeFacadeServiceImpl implements ISMSCodeFacadeService {

    @Resource
    private SMSUtil smsUtil;
    @Resource
    private IMsgTemplateService msgTemplateService;

    @Override
    public SMSAuthCodeResDto sendSMSAuthCode(SMSAuthCodeParamDto paramDto) throws Exception {
        String hotelGroupCode = paramDto.getLhotelGroupCode();
        String mobile = paramDto.getMobile();
        String templateCode = paramDto.getTemplateCode();
        if(!smsUtil.isMobiPhoneNum(mobile)){
            throw new SysException(BaseSysExceptionEnum.INVALID_PARA.getCode(), BaseSysExceptionEnum.INVALID_PARA.getMsg()+"电话号码不合法", BaseSysExceptionEnum.INVALID_PARA.getBizType());
        }
        if(StringUtil.isBlank(hotelGroupCode)){
            throw new SysException(BaseSysExceptionEnum.INVALID_PARA.getCode(), BaseSysExceptionEnum.INVALID_PARA.getMsg()+"[hotelGroupCode]:"+hotelGroupCode, BaseSysExceptionEnum.INVALID_PARA.getBizType());
        }
        //生成验证码
        String verificationCode = smsUtil.randomCode_6();
        //发送验证短信
        sendMsg(verificationCode,mobile,hotelGroupCode,templateCode);
        // 缓存验证码，生成msgId并返回，返回给用户用于验证
        String msgId = smsUtil.cacheVerificationCode(verificationCode,templateCode, mobile,hotelGroupCode);
        SMSAuthCodeResDto res = new SMSAuthCodeResDto();
        res.setMsgId(msgId);
        return res;
    }

    /**
     * 验证是否通过
     *
     * @param paramDto
     */
    @Override
    public void verifyCode(VerifyCodeParamDto paramDto) {
        String verificationCode = paramDto.getVerificationCode();
        String hotelGroupCode = paramDto.getLhotelGroupCode();
        String mobile = paramDto.getMobile();
//        String templateCode = MsgConstant.TEMPLET_CODE.Registration_Verification;
        String templateCode = paramDto.getTemplateCode();
        if(!smsUtil.isMobiPhoneNum(mobile)){
            throw new SysException(BaseSysExceptionEnum.INVALID_PARA.getCode(), BaseSysExceptionEnum.INVALID_PARA.getMsg()+"电话号码不合法", BaseSysExceptionEnum.INVALID_PARA.getBizType());
        }
        if(StringUtil.isBlank(hotelGroupCode)){
            throw new SysException(BaseSysExceptionEnum.INVALID_PARA.getCode(), BaseSysExceptionEnum.INVALID_PARA.getMsg()+"[hotelGroupCode]:"+hotelGroupCode, BaseSysExceptionEnum.INVALID_PARA.getBizType());
        }
        //生成msgId
        String msgId = smsUtil.getMsgId(mobile,hotelGroupCode,templateCode);
        //查询
        smsUtil.verifyCode(msgId,verificationCode,30);
    }

    private void sendMsg(String verificationCode,String mobile,String lhotelGroupCode,String templateCode) throws Exception {
        L2RGroupBaseInfoDto info = HotelUtil.getGroupBaseinfo(lhotelGroupCode);
        if(info == null){
            throw new SysException(BaseSysExceptionEnum.DATA_IS_NOT_EXIST,"酒店基本信息在缓存未命中");
        }

        String groupName = info.getName();
        //获得模板
        MsgTemplate template = msgTemplateService.findTemplate(lhotelGroupCode, null, MsgConstant.CHANNEL.DAYU, templateCode);
        Map map = new HashMap<>();
        map.put("code",verificationCode);
//        map.put("product",groupName);
        String str = FreemarkerUtil.getMsg(template,map );

        smsUtil.sendMsg(template.getDayuSignName(),lhotelGroupCode,str,template,mobile);
    }
}
